package com.wtwd.campus.receiver;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.annotation.MsgType;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.entity.DeviceInfo;
import com.wtwd.campus.entity.DeviceSetting;
import com.wtwd.campus.entity.MqIpAndPortResult;
import com.wtwd.campus.entity.MqMessage;
import com.wtwd.campus.h5_manager.entity.DeviceLog;
import com.wtwd.campus.h5_manager.service.DeviceLogService;
import com.wtwd.campus.service.DeviceInfoService;
import com.wtwd.campus.service.DeviceSettingService;
import com.wtwd.campus.utils.ReceiverUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@MsgType(value = Constants.SET_TEMPORARY_CONTROL_CENTER)
public class TemporaryControlCenterReceiverHandler implements IReceiverHandler {
    @Autowired
    private DeviceInfoService deviceInfoService;
    @Autowired
    private ReceiverUtils receiverUtils;
    @Autowired
    private DeviceSettingService deviceSettingService;
    @Autowired
    private DeviceLogService deviceLogService;
    @Override
    public void handlerMessage(MqMessage mqMessage) {
        MqIpAndPortResult mqIpAndPortResult = (MqIpAndPortResult) mqMessage.getObject();
        String imei = mqIpAndPortResult.getImei();
        String commandNumber = mqIpAndPortResult.getCommandNumber();
        String ip = mqIpAndPortResult.getIp();
        Integer port = mqIpAndPortResult.getPort();
        //处理指令回复的时间
        String deviceDate = mqIpAndPortResult.getDeviceDate();
        String deviceTime = mqIpAndPortResult.getDeviceTime();
        Long time = getTime(deviceDate, deviceTime);
        DeviceInfo deviceInfo = deviceInfoService.getDeviceByImei(imei);
        if (deviceInfo != null) {
            if (mqIpAndPortResult.getMqLocationData() != null) {
                if (mqIpAndPortResult.getMqLocationData().getLatitude() != 0 && mqIpAndPortResult.getMqLocationData().getLongitude() != 0) {
                    //进行定位数据存储以及围栏告警记录的存储
                    deviceInfo = receiverUtils.dealLocation(mqIpAndPortResult.getMqLocationData(), time, deviceInfo, 1);
                }
            }
            //修改设备信息
            deviceInfo.setLastUpTime(System.currentTimeMillis());
            deviceInfoService.modifyDeviceInfo(deviceInfo);
            //修改设备设置信息
            DeviceSetting deviceSetting = deviceSettingService.getDeviceSettingByImei(imei);
            if (deviceSetting != null) {
                deviceSetting.setIpAddress(ip);
                deviceSetting.setPort(port);
                deviceSettingService.modifyDeviceSetting(deviceSetting);
            }
            //删除缓存中的指令
            String delKey = Constants.SET_TEMPORARY_CONTROL_CENTER.concat("-").concat(commandNumber);
            receiverUtils.deleteCommandInRedis(imei, delKey);
            //完善指令下发记录
            receiverUtils.completeCommandRecord(Constants.SET_TEMPORARY_CONTROL_CENTER, imei, commandNumber, null, Constants.COMMAND_COMPLETE, time);
            //存储设备接收日志
            DeviceLog deviceLog = new DeviceLog();
            deviceLog.setSendUser(Constants.DEVICE_USER);
            deviceLog.setImei(imei);
            deviceLog.setBigType(Constants.MESS_TYPE);
            deviceLog.setType(Constants.SET_TEMPORARY_CONTROL_CENTER);
            JSONObject contant = new JSONObject();
            contant.put("设备号", imei);
            contant.put("ip",ip);
            contant.put("端口",port);
            contant.put("指令回复时间 时分秒",time);
            contant.put("日月年",mqIpAndPortResult.getDeviceDate());
            contant.put("经度", mqIpAndPortResult.getMqLocationData().getLongitude());
            contant.put("纬度", mqIpAndPortResult.getMqLocationData().getLatitude());
            contant.put("速度", mqIpAndPortResult.getMqLocationData().getSpeed());
            contant.put("方位角", mqIpAndPortResult.getMqLocationData().getOrientation());
            contant.put("设备状态",mqIpAndPortResult.getDeviceStatus());
            deviceLog.setCmd(contant.toJSONString());
            deviceLog.setSendTime(System.currentTimeMillis());
            deviceLog.setCommandNumber(commandNumber);
            deviceLog.setState(1);
            deviceLogService.addDeviceLog(deviceLog);
        } else {
            saveUnInStorage(imei);
            receiverUtils.cloaseChannel(imei);
        }
    }
}
